using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using Blazing.BusinessObjects.Cms;
using Iesi.Collections.Generic;
using System.BusinessObjects.With;
using System.BusinessObjects.Transactions;
using System.BusinessObjects.Data;
using System.Diagnostics;

namespace Blazing.WebFacade
{
    [System.ComponentModel.DataObject]
    public class ThemeController
    {
        #region Masterpages
        [DataObjectMethodAttribute(DataObjectMethodType.Delete)]
        public void DeleteMasterpage(Masterpage masterpage)
        {
            Masterpage objMaster = Masterpage.Load(masterpage.ID);
            objMaster.Delete();
            objMaster.Save(SaveMode.Flush);
            Site.Evict(objMaster.Site.ID);
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Insert)]
        public Masterpage NewMasterpage(Masterpage masterpage)
        {
            masterpage.Save(SaveMode.Flush);
            Site.Evict(masterpage.Site.ID);
            return masterpage;
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Update)]
        public Masterpage UpdateMasterpage(Masterpage masterpage)
        {
            masterpage.Save(SaveMode.Flush);
            Site.Evict(masterpage.Site.ID);
            return masterpage;
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public ISet<Masterpage> GetMasterpages(int siteId)
        {
            Site obj = Site.Load(siteId);
            return obj != null ? obj.Masterpages : null;
        }
        #endregion

        #region Themes
        [DataObjectMethodAttribute(DataObjectMethodType.Delete)]
        public Theme DeleteTheme(Theme theme)
        {
            Theme toDel = Theme.Load(theme.ID);
            
            foreach (Theme t in toDel.Masterpage.Themes)
                if (t.ID == toDel.ID)
                {
                    toDel.Masterpage.Themes.Remove(t);
                    break;
                }

            toDel.Masterpage.Evict();
            toDel.Delete();
            toDel.Save(SaveMode.Flush);

            Debug.Assert(toDel.RowState == System.Data.DataRowState.Deleted);

            return toDel;
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Insert)]
        public Theme NewTheme(Theme theme)
        {
            theme.Save();
            Masterpage.Evict(theme.Masterpage.ID);

            return theme;
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Update)]
        public Theme UpdateTheme(Theme theme)
        {
            Theme saveTheme = Theme.Load(theme.ID);

            saveTheme.Name = theme.Name;
            saveTheme.Path = theme.Path;

            //Theme.Evict(theme.ID);
            saveTheme.Save(SaveMode.Flush);

            Masterpage.Evict(saveTheme.Masterpage.ID);

            return theme;
        }

        /// <summary>
        /// Gets a list of themes are a masterpage
        /// </summary>
        /// <param name="ID">id of masterpage</param>
        /// <returns></returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public ISet<Theme> GetThemes(int ID)
        {
            Masterpage obj = Masterpage.Load(ID);
            return obj != null ? obj.Themes : null;
        }
        #endregion
    }
}
